﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace PTA.Net
{
    /// <summary>
    /// 通用邮件处理类
    /// </summary>
    public static class MailClient
    {
        #region 发送电子邮件

        /// <summary>
        /// 发送电子邮件(端口:25,不加密)
        /// </summary>
        /// <param name="smtpServer">电子邮件发送服务器(SMTP)</param>
        /// <param name="userName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="mail">邮件</param>
        /// <example >
        /// SendMail("smtp.foxmail.com", "lenoval", "password", "lenoval@foxmail.com", "lenoval@126.com", "Titel", "body"))
        /// </example>
        /// <returns>返回一个值,指示是否成功发送</returns>
        public static bool Send(string smtpServer, string userName, string pwd, MailMessage mail)
        {
            SmtpClient client = new SmtpClient();
            client.Host = smtpServer;
            client.Port = 25;
            client.Credentials = new System.Net.NetworkCredential(userName, pwd);
            client.EnableSsl = false;
            return Send(client, mail);
        }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpServer">电子邮件发送服务器(SMTP)</param>
        /// <param name="port">端口号，默认25</param>
        /// <param name="userName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="enableSsl">是否使用安全的连接</param>
        /// <param name="mail">邮件</param>
        /// <example >
        /// SendMail("smtp.foxmail.com", "lenoval", "password", "lenoval@foxmail.com", "lenoval@126.com", "Titel", "body"))
        /// </example>
        /// <returns>返回一个值,指示是否成功发送</returns>
        public static bool Send(string smtpServer, int port, string userName, string pwd, bool enableSsl, MailMessage mail)
        {
            SmtpClient client = new SmtpClient();
            client.Host = smtpServer;
            client.Port = port;
            client.Credentials = new System.Net.NetworkCredential(userName, pwd);
            client.EnableSsl = enableSsl;
            return Send(client, mail);
        }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="client">客户端信息</param>
        /// <param name="mail">邮件信息</param>
        /// <returns>返回一个值,指示是否发送成功</returns>
        public static bool Send(SmtpClient client, MailMessage mail)
        {
            bool result = false;
            if (client != null && mail != null)
            {
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                try
                {
                    client.Send(mail);
                    result = true;
                }
                catch (Exception ex)
                {
                    string msg = string.Format("发送邮件错误,错误信息:{1}{0}SMTP:{2}{0}Port:{3}{0}UserName:{4}{0}Password:{5}{0}EnableSsl:{6}",
                     Environment.NewLine,
                     ex.Message,
                     client.Host,
                     client.Port,
                     ((System.Net.NetworkCredential)client.Credentials).UserName,
                     ((System.Net.NetworkCredential)client.Credentials).Password,
                     client.EnableSsl);
                    throw new Exception(msg, ex);
                }
            }
            return result;
        }
        #endregion

        #region POP3接收邮件


        #endregion
    }
}
